import feedforward
import backprop
import net

alphabet = ['#','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','-', "'", '.']

encoders  = {}
mom_encoders = {}

def make_encoder(encoding_size= len(alphabet), valuegen = None):
    if valuegen is None:
        valuegen = feedforward.default_vgen
    return feedforward.generate_net([1, encoding_size], valuegen)

mom_encoders = dict((a, make_encoder(valuegen = lambda: 0.0))\
                            for a in alphabet)


def gen_encoders(encoding_size = len(alphabet), alphabet = alphabet):
    global encoders
    encoders = {}
    layers = [1,encoding_size]
    for a in alphabet:
        encoders[a] = make_encoder(encoding_size)

    return encoders

gen_encoders()

def prop_coder(symbol, transfer):
    return feedforward.propagate([1.0], encoders[symbol], transfer)

LEARNRATE = 0.1
MOMRATE = 0.24

def bprop_coder(symbol, deltas, transfer_deriv):
    (out, pre_activ, post_activ) = feedforward.propagate([1.0], encoders[symbol])
    mom_encoders[symbol] = backprop.sqrt_backprop_deltas(encoders[symbol], deltas,\
             pre_activ, post_activ, mom_encoders[symbol], LEARNRATE,\
             MOMRATE, transfer_deriv)[0]

def bprop_output(symbol, errors, transfer_deriv):
    (out, pre_activ, post_activ) = feedforward.propagate([1.0], encoders[symbol])
    mom_encoders[symbol] = backprop.sqrt_backprop(encoders[symbol], errors,\
             pre_activ, post_activ, mom_encoders[symbol], LEARNRATE,\
             MOMRATE, transfer_deriv)[0]
